package com.example.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.Sqlitedatabase.OrlSqlDatabase;
import com.example.myapplication.Sqlitedatabase.Q1Bean;
import com.example.myapplication.bean.Q1;
import com.google.gson.Gson;
import com.j256.ormlite.dao.Dao;

import org.json.JSONException;
import org.json.JSONObject;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Q1Activity extends AppCompatActivity implements View.OnClickListener {

    private TextView money;
    private Spinner car_num;
    private EditText topup;
    private Button query;
    private Button topupbutton;
    RequestQueue queue = null;
    Context context;
    private String TAG = "init";
    String s = "1";
    private RelativeLayout visiable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_q1);
        initView();
        context = this;
        queue = Volley.newRequestQueue(context);
        try {
            //默认查询一号车
            request(Integer.valueOf(s));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initView() {
        money = (TextView) findViewById(R.id.money);
        car_num = (Spinner) findViewById(R.id.car_num);
        topup = (EditText) findViewById(R.id.topup);
        query = (Button) findViewById(R.id.query);
        topupbutton = (Button) findViewById(R.id.topupbutton);

        car_num.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.e(TAG, "onItemSelected: " + parent.getItemAtPosition(position).toString());
                s = parent.getItemAtPosition(position).toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        query.setOnClickListener(this);
        topupbutton.setOnClickListener(this);
        visiable = (RelativeLayout) findViewById(R.id.visiable);
        visiable.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.query:
                try {
                    //默认查询一号车
                    request(Integer.valueOf(s));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.topupbutton:
                Editable text = topup.getText();
                int i = Integer.valueOf(text.toString());
                if(i>999 || i<1){
                    Toast.makeText(context,"只能输入1-999",Toast.LENGTH_SHORT).show();
                    topup.setText("");
                }else {
                    CharSequence text1 = money.getText();
                    int balance = Integer.valueOf(text1.toString());
                    int all =balance+i;
                    money.setText(all+"");
                    Toast.makeText(context,"充值成功",Toast.LENGTH_SHORT).show();
                    OrlSqlDatabase instance = OrlSqlDatabase.getInstance(context);
                    try {

                        Dao<Q1Bean, ?> dao = instance.getDao(Q1Bean.class);

                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
                        Date date = new Date();
                        String format = simpleDateFormat.format(date);


                        dao.create(new Q1Bean("车牌号",all,"管理员",format));

                        List<Q1Bean> q1Beans = dao.queryForAll();

                        for (Q1Bean q1Bean : q1Beans){
                            Log.e(TAG, "onClick: "+q1Bean.toString() );
                        }

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

                break;
        }
    }

    private void submit() {
        // validate
        String topupString = topup.getText().toString().trim();
        if (TextUtils.isEmpty(topupString)) {
            Toast.makeText(this, "topupString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }


    public void request(int index) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        JSONObject id = jsonObject.put("id", index);
        visiable.setVisibility(View.VISIBLE);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                1,
                "http://shop.hwt.ink:7300/mock/5e1bf27770f865002026ca09/intercace/carbalancequery",
                id.toString(),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            Log.e("init", "onResponse: " + jsonObject.getString("a"));
                            String data = jsonObject.getString("a");
                            Gson gson = new Gson();
                            final Q1 q1 = gson.fromJson(data, Q1.class);
                            Log.e("init", "onResponse: " + q1.getMoney());

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    money.setText(q1.getMoney() + "");
                                    visiable.setVisibility(View.INVISIBLE);
                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                }
        );
        queue.add(jsonObjectRequest);

    }
}
